package nc.hw;

import java.util.Arrays;

public class HJ36 {

    private static char[] alpha(String key) {
        key = key.toLowerCase();
        // key得到新的字母表
        char[] alpha = new char[26];
        int[] checkExists = new int[26];
        int i = 0;
        char[] chars = key.toCharArray();
        for (char aChar : chars) {
            if (checkExists[aChar - 'a'] == 0) {
                alpha[i] = aChar;
                checkExists[aChar - 'a'] = 1;
                i ++;
            }
        }
        for (int i1 = 0; i1 < checkExists.length; i1++) {
            if (checkExists[i1] == 0) {
                // 不存在，放进去字母表
                alpha[i] = (char) (i1 + 'a');
                i ++;
            }
        }
        return alpha;
    }

    private static String encode(String key, String word) {
        char[] alpha = alpha(key);
        StringBuilder stringBuilder = new StringBuilder();
        for (char c : word.toCharArray()) {
            stringBuilder.append(alpha[c - 'a']);
        }
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        System.out.println(encode("nihao", "ni"));
    }

}
